<?php

namespace app\admin\controller\rush;

use app\common\controller\Backend;

/**
 * 收益
 *
 * @icon fa fa-circle-o
 */
class OrderEntrust extends Backend
{

    /**
     * RushOrderEntrust模型对象
     * @var \app\api\model\RushOrderEntrust
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\RushOrderEntrust;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 统计
     */
    public function statistic()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            //过滤昵称和手机号默认条件
            $filter = $this->request->get("filter", '');
            $op = $this->request->get("op", '', 'trim');
            $filter = (array)json_decode($filter, true);
            $op = (array)json_decode($op, true);
            $map = [
                'a.pay_status' => '1'
            ];
            if(isset($filter['nickname'])){
                $map['c.nickname'] = $filter['nickname'];
                unset($filter['nickname'],$op['nickname']);
                $this->request->get(['filter' => json_encode($filter)]);
                $this->request->get(['op' => json_encode($op)]);
            }
            if(isset($filter['mobile'])){
                $map['c.mobile'] = $filter['mobile'];
                unset($filter['mobile'],$op['mobile']);
                $this->request->get(['filter' => json_encode($filter)]);
                $this->request->get(['op' => json_encode($op)]);
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->alias('a')
                ->join('rush_order b','b.id = a.order_id')
                ->join('user c','c.id = b.buyer_id')
                ->where($where)
                ->where($map)
                ->field('b.buyer_id,c.nickname,c.mobile,sum(a.profit) profit_total')
                ->group('b.buyer_id')
                ->paginate($limit)
                ->each(function($v) use($where,$map) {
                    //今日收益
                    $v['profit_today'] = $this->model
                        ->alias('a')
                        ->join('rush_order b','b.id = a.order_id')
                        ->where($where)
                        ->where('b.buyer_id',$v['buyer_id'])
                        ->where('a.pay_status','1')
                        ->whereTime('a.profit_time', 'today')
                        ->sum('a.profit');
                });

            //今日总收益
            $profit_today = $this->model
                ->alias('a')
                ->join('rush_order b','b.id = a.order_id')
                ->join('user c','c.id = b.buyer_id')
                ->where($where)
                ->where($map)
                ->whereTime('a.profit_time', 'today')
                ->sum('a.profit');

            $result = array("total" => $list->total(), "rows" => $list->items(), "profit_today" => $profit_today);

            return json($result);
        }
        return $this->view->fetch();
    }
}
